//
//  StarterModuleViewController.m
//  Learn
//
//  Created by 迟建国 on 2024/4/5.
//  Copyright © 2024 迟建国. All rights reserved.
//

#import "StarterModuleViewController.h"
#import "ChapterDetailTableViewCell.h"
#import "LodingView.h"
#import "BookReadTypeView.h"
#import "SynchronousTypeView.h"
#import "HomeRequest.h"
#import "HSTextBookUnitWordModel.h"
#import "WordFollowViewController.h"
#import "TextFollowViewController.h"

@interface StarterModuleViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArr;
@end

@implementation StarterModuleViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = self.unitModel.name;
    self.view.backgroundColor = rgba(229, 229, 229, 1);
    UIButton *saveBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    saveBtn.frame = CGRectMake(0, 0, 44, 44);
    [saveBtn setImage:[UIImage imageNamed:@"icon_save"] forState:UIControlStateNormal];
    [saveBtn addTarget:self action:@selector(save) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:saveBtn];
    [self setUI];
    [self loadData];
    // Do any additional setup after loading the view.
}

- (void)loadData {
    [HomeRequest getTextBookUnitWorkWithId:self.unitModel.unitid Result:^(NSMutableArray<HSTextBookUnitWordModel *> *list) {
        self.dataArr = list;
        [self.tableView reloadData];
    }];
}

- (void)save {
//    [LodingView lodingWithVc:self Complection:^(NSString * _Nonnull str) {
//        [NSObject.getKeyWindowRootViewController gq_removeBgView];
//    }];
//    [BookReadTypeView chooseTypeWithVc:self Complection:^(NSString * _Nonnull str) {
//
//    }];
    [SynchronousTypeView chooseTypeWithVc:self Complection:^(NSString * _Nonnull str) {

    }];
}

- (void)setUI {
    self.tableView=[[UITableView alloc]initWithFrame:CGRectMake(0,NavigationBarHeight, SCREEN_W, SCREEN_H - NavigationBarHeight - SafeAreaBottomHeight) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    if (@available(iOS 13.0, *)) {
        [self.tableView setAutomaticallyAdjustsScrollIndicatorInsets:NO];
    } else {
        // Fallback on earlier versions
    }
    if (@available(iOS 15.0, *)) {
        self.tableView.sectionHeaderTopPadding = 0;
    } else {
        // Fallback on earlier versions
    }
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    self.tableView.backgroundColor=[UIColor whiteColor];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.delegate=self;
    self.tableView.dataSource=self;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.showsHorizontalScrollIndicator = NO;
    [self.tableView registerNib:[UINib nibWithNibName:@"ChapterDetailTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"ChapterDetailTableViewCell"];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArr.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 83;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ChapterDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ChapterDetailTableViewCell" forIndexPath:indexPath];
    if (!cell) {
        cell = [[[NSBundle mainBundle]loadNibNamed:@"ChapterDetailTableViewCell" owner:self options:nil] lastObject];
    }
//    if (self.type == BookTypeWrittenExam) {
//        cell.model = self.model.sr[indexPath.row];
//    } else {
//        cell.model = self.model.hsr[indexPath.row];
//    }
    HSTextBookUnitWordModel *model = self.dataArr[indexPath.row];
    cell.nameL.text = model.moduleName;
    cell.typeL.text = model.resourceName;
    cell.unitLB.text = nil;
    cell.icon.image = [UIImage imageNamed:model.moduleId==30 ? @"icon_listenwrite" : @"icon_record"];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    HSTextBookUnitWordModel *model = self.dataArr[indexPath.row];
    [SynchronousTypeView chooseTypeWithVc:self Complection:^(NSString * _Nonnull str) {
        if ([str isEqualToString:@"跟读"]) {
            if ([model.moduleName isEqualToString:@"同步跟读-课文"]) {
                TextFollowViewController *vc = [[TextFollowViewController alloc] init];
                vc.unitWordModel = model;
                [self.navigationController pushViewController:vc animated:YES];
            } else if ([model.moduleName isEqualToString:@"同步跟读-单词"]) {
                WordFollowViewController *vc = [[WordFollowViewController alloc] init];
                vc.unitWordModel = model;
                vc.type = @"跟读";
                [self.navigationController pushViewController:vc animated:YES];
            } else {
                
            }

        }
        
        if ([str isEqualToString:@"朗读"]) {
            if ([model.moduleName isEqualToString:@"同步跟读-课文"]) {
                TextFollowViewController *vc = [[TextFollowViewController alloc] init];
                vc.unitWordModel = model;
                [self.navigationController pushViewController:vc animated:YES];
            } else if ([model.moduleName isEqualToString:@"同步跟读-单词"]) {
                WordFollowViewController *vc = [[WordFollowViewController alloc] init];
                vc.unitWordModel = model;
                vc.type = @"朗读";
                [self.navigationController pushViewController:vc animated:YES];
            } else {
                
            }
        }
        
        if ([str isEqualToString:@"背诵"]) {

        }

    }];
    
}

- (NSMutableArray *)dataArr {
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}
@end
